﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIMS.Global;
using SIMS.Database;
using System.Collections;
using System.Data;

namespace SIMS.School
{
    public class DepartmentDivision
    {
        public int Id { get; set; }
        public int CompanyId { get; set; }

        public string Name { get; set; }
        public string CompanyName { get; set; }

        public string Description { get; set; }
        public string DepartmentHead { get; set; }

        public bool IsActive { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime ModifiedDate { get; set; }

        public MyError Error { get; set; }
    }

    public class DepartmentDivisionDB
    {
        MyDatabase _db = new MyDatabase();

        public DepartmentDivision Save(DepartmentDivision DepartmentDivision)
        {
            try
            {
                SortedList _param = new SortedList();
                _param.Add("@Id", DepartmentDivision.Id);
                _param.Add("@CompanyId", DepartmentDivision.CompanyId);

                _param.Add("@Name", DepartmentDivision.Name);

                if (DepartmentDivision.Description != string.Empty)
                {
                    _param.Add("@Description", DepartmentDivision.Description);
                }
                else
                {
                    _param.Add("@Description", DBNull.Value);
                }

                if (DepartmentDivision.DepartmentHead != string.Empty)
                {
                    _param.Add("@DepartmentHead", DepartmentDivision.DepartmentHead);
                }
                else
                {
                    _param.Add("@DepartmentHead", DBNull.Value);
                }

                _param.Add("@IsActive", DepartmentDivision.IsActive);

                int _affRow = _db.ExecuteNonSqlSP("usp_DepartmentDivisionInsertUpdateSingleItem", _param);

                if (_affRow > 0)
                {
                    DepartmentDivision.Error.ErrorNo = 0;
                }
                else
                {
                    DepartmentDivision.Error.ErrorNo = 1;
                    DepartmentDivision.Error.ErrorMessage = "No row affect when saving department division details.";
                }
            }
            catch (Exception Ex)
            {
                DepartmentDivision.Error.ErrorNo = 10002;
                DepartmentDivision.Error.ErrorLocation = "DepartmentDivision";
                DepartmentDivision.Error.ErrorMessage = Ex.Message;
                DepartmentDivision.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "DepartmentDivision");
            }

            return DepartmentDivision;
        }

        public DepartmentDivision GetDepartmentDivision(int Id, int CompanyId)
        {
            DepartmentDivision retValue = new DepartmentDivision();
            retValue.Error = new MyError();
            try
            {
                DataTable _dt = new DataTable("tblDepartmentDivision");

                SortedList _param = new SortedList();
                _param.Add("@Id", Id);
                _param.Add("@CompanyId", CompanyId);

                _dt = _db.GetDataTableBySP("usp_DepartmentDivisionSelectSingleItem", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    FillData(_dt.Rows[0], retValue);
                }
            }
            catch (Exception Ex)
            {
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "DepartmentDivision";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "DepartmentDivision");
            }

            return retValue;
        }

        private void FillData(DataRow aRow, DepartmentDivision DepartmentDivision)
        {
            DepartmentDivision.Id = Convert.ToInt32(aRow["Id"].ToString());
            DepartmentDivision.Error = new MyError();
            
            DepartmentDivision.Name = aRow["Name"].ToString();

            if (aRow["Description"] != DBNull.Value)
            {
                DepartmentDivision.Description = aRow["Description"].ToString();
            }
            else
            {
                DepartmentDivision.Description = string.Empty;
            }

            if (aRow["DepartmentHead"] != DBNull.Value)
            {
                DepartmentDivision.DepartmentHead = aRow["DepartmentHead"].ToString();
            }
            else
            {
                DepartmentDivision.DepartmentHead = string.Empty;
            }

            DepartmentDivision.IsActive = Convert.ToBoolean(aRow["IsActive"].ToString());

            DepartmentDivision.Error.ErrorNo = 0;
        }
    }
}
